
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
extern int sgemm_mNK(int m, int n, int k, float alpha, float *a, int lda, float *b, int ldb, float beta, float *c, int ldc);

int main(int argc, char* argv[]){

    if (argc != 4) {
        // 处理参数不足的情况
        // 打印错误信息或采取适当的操作
        printf("Usage: %s m k n\n", argv[0]); // 打印使用方法
        return 1; // 返回 1 表示错误
    }

    double gflops;
    double timeuse;
    struct timeval tpstart, tpend;

    int m = atoi(argv[1]); // 第一个参数
    int k = atoi(argv[2]); // 第二个参数
    int n = atoi(argv[3]); // 第三个参数

    int lda=k;
    int ldb=n;    //Row-major
    int ldc=n;

    // int lda=m;
    // int ldb=k;
    // int ldc=m;  //Column-major

    float alpha = 2.0;
    float beta = 0.0;

    //m=n=k= j

    // const size_t alignment = 64;
    // float* a = (float*)aligned_alloc(alignment, m * k * sizeof(float));
    // float* b = (float*)aligned_alloc(alignment, k * n * sizeof(float));
    // float* c = (float*)aligned_alloc(alignment, m * n * sizeof(float));
    
    // if (!a || !b || !c) {
    //     if (a) free(a);
    //     if (b) free(b);
    //     if (c) free(c);
    //     printf("Memory allocation failed!\n");
    // }
    float* a;
    float* b;
    float* c;
    a = (float*)malloc(sizeof(float) * m * k);
    b = (float*)malloc(sizeof(float) * k * n);
    c = (float*)malloc(sizeof(float) * m * n);

    //初始化
    for(int p = 0; p < m * k; p++){
        a[p] = 1.0;
    }
    for(int q = 0; q < k * n; q++){
        b[q] = 0.5;
    }
    for(int r = 0; r < m * n; r++){
        c[r] = 1.0;
    }

    gettimeofday(&tpstart, NULL);
    if(Model_Predict){sgemm_mNK(m, n, k, alpha, a, lda, b, ldb, beta, c, ldc);}
    else{return 0;}
    gettimeofday(&tpend, NULL);
    timeuse = ((double)1000000*(tpend.tv_sec-tpstart.tv_sec) + (double)(tpend.tv_usec-tpstart.tv_usec))/1000000;

    //计算Gflops
    gflops = 2*m*n*k;
    gflops = gflops/timeuse*1.0e-9;
    //printf("timeuse: %f\n", timeuse);
    printf("%6.6f",gflops);

    printf("\n");

    // for(int i = 0; i < m; i++){
    //     for(int j = 0; j < n; j++){
    //         printf("%10.2f", c[i * n + j]);   //RowMajor Print
    //     }
    //     printf("\n");
    // }

    //printf("PASS!\n");
    free(a);
    free(b);
    free(c);
    return 0;
}


